package cn.gaily.eduucenter.service.impl;

import cn.gaily.commonutils.JwtUtils;
import cn.gaily.commonutils.MD5;
import cn.gaily.eduucenter.entity.UcenterMember;
import cn.gaily.eduucenter.entity.vo.RegisterVo;
import cn.gaily.eduucenter.mapper.UcenterMemberMapper;
import cn.gaily.eduucenter.service.UcenterMemberService;
import cn.gaily.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xuchengwei
 * @since 2021-08-04
 */
@Slf4j

@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取登录手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001, "登录失败,未填写手机号或密码");
        }
        //判断手机号是否存在
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",ucenterMember.getMobile());
        UcenterMember res = baseMapper.selectOne(wrapper);
        if(res == null){
            throw new GuliException(20001, "登录失败，未注册");
        }
        //判断密码是否正确
        if(!res.getPassword().equals(MD5.encrypt(password))) {
            throw new GuliException(20001, "登录失败，密码错误");
        }
        //判断是否被禁用
        if(res.getIsDisabled() == true) {
            throw new GuliException(20001, "登录失败，被禁用");
        }
        //生成token
        String token = JwtUtils.getJwtToken(res.getId(), res.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String nickname = registerVo.getNickname();
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password) || StringUtils.isEmpty(nickname)){
            throw new GuliException(20001,"请填写完整信息");
        }
        //判断验证码是否一样
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new GuliException(20001,"验证码错误");
        }
        //验证手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"手机号已存在");
        }
        //密码摘要
        registerVo.setPassword(MD5.encrypt(password));
        UcenterMember memberBean = new UcenterMember();
        BeanUtils.copyProperties(registerVo,memberBean);
        memberBean.setIsDisabled(false);
        baseMapper.insert(memberBean);

    }

    @Override
    public UcenterMember getByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return baseMapper.selectOne(wrapper);
    }
}
